<?php 
//-----------------------------------------------------------
//     NumbWordter - Number to Word converter class in PHP
//   Written by Manda Krishna Srikanth (krishnasrikanth.com)
//-----------------------------------------------------------
class numtoword
{
	private $zero = array('0'=>'Zero');
	
	private $discrete=array('1'=>'one',			'2'=>'two',			'3'=>"three",		'4'=>"four",		'5'=>"five",
	                        '6'=>'six',			'7'=>'seven',		'8'=>'eight',		'9'=>'nine',		'10'=>'ten',
							'11'=>'eleven',		'12'=>'twelve',		'13'=>'thirteen',	'14'=>'fourteen',	'15'=>'fifteen',
							'16'=>'sixteen',	'17'=>'seventeen',	'18'=>'eighteen',	'19'=>'nineteen',	'-'=>'minus', 
							'01'=>'one',		'02'=>'two',		'03'=>"three",		'04'=>"four",		'05'=>"five",
							'06'=>'six',		'07'=>'seven',		'08'=>'eight',		'09'=>'nine', 		'000'=>'', 
							'001'=>'One', 		'002'=>'two',		'003'=>"three",		'004'=>"four",		'005'=>"five",
							'006'=>'six',		'007'=>'seven',		'008'=>'eight',		'009'=>'nine');
	
	private $ten_digit_prefix=array('2'=>'twenty',	'3'=>'thirty',	'4'=>'forty',	'5'=>'fifty',
									'6'=>'sixty',	'7'=>'seventy',	'8'=>'eighty',	'9'=>'ninty');
	
	private $mool_array=array('',"thousand","million","billion","trillion","quadrillion","quintillion",
								"sextillion","septillion","octillion","nonillion","decillion",
								"unidecillion","duodecillion","tredecillion","quattuordecillion");

	private $sentence; //final sentence
	private $error; //error if generated

	//methods
	private function twodigits($num)
	{
		//displays from 1 to 99
		if($num==0)
		{
			return $this->zero[$num];
		}elseif($num<20)
		{
			return $this->discrete[$num];
		}
		elseif($num==20 or $num==30 or $num==40 or $num==50 or $num==60 or $num==70 or $num==80 or $num==90)
		{
			return $this->ten_digit_prefix[substr($num,0,1)];
		}
		else
		{
			return $this->ten_digit_prefix[substr($num,0,1)].' '.$this->discrete[substr($num,1,1)];
		}
	}
	
	//displays three digit numbers
	private function threedigits($num,$count)
	{
		if($num==000)
		{
			return $this->discrete[000];
		}elseif($num==100 or $num==200 or $num==300 or $num==400 or $num==500 or $num==600 or $num==700 or $num==800 or $num==900)
		{
			if($count==0){
				return $this->discrete[substr($num,0,1)].' hundred';
			}else{
				return $this->discrete[000].' and '.$this->discrete[substr($num,0,1)].' hundred';
			}
			
		}elseif($num==001 or $num==002 or $num==003 or $num==004 or $num==005 or $num==006 or $num==007 or $num==008 or $num==009)
		{
			if($count==0){
				return $this->discrete[$num];
			}else{
				return $this->discrete[000].' and '.$this->discrete[$num];
			}
			
		}elseif(substr($num,0,1)=='0')
		{
			if($count==0){
				return $this->twodigits(substr($num,1,2));
			}else{
				return $this->discrete[000].' and '.$this->twodigits(substr($num,1,2));
			}
			
		}else
		{
			if($count==0){
				return $this->discrete[substr($num,0,1)].' hundred and '.$this->twodigits(substr($num,1,2));
			}else{
				return $this->discrete[000].' and '.$this->discrete[substr($num,0,1)].' hundred and '.$this->twodigits(substr($num,1,2));
			}
		}
	}
	
	private function decider($num,$count)
	{
		if(strlen($num)<=2)
			return $this->twodigits($num,$count);
		else
			return $this->threedigits($num,$count);
	}
	
	public function en($num)
	{
		//return if more than 48 digits
		if(strlen($num)>48) 
		{
			$this->error="Number out of bounds";
			return $this->error;
		}
		
		//check if first 
		if(substr($num,0,1)=="-")
		{
			$this->sentence.='minus ';
			$num=substr($num,1,strlen($num)-1);
		}
		
		if(strlen($num)<=3)
		{
			$this->sentence.=$this->decider($num,0);
		}
		else
		{
			$k=strrev($num);
			for($i=0;$i<strlen($k);$i=$i+3){$arro[]=strrev(substr($k,$i,3));}
			//reverse again
			$arro=array_reverse($arro);
			//print_r($arro);
			$mool=ceil(strlen($num)/3);
			if((strlen(num)%3)==0)
			$i==1;
			{$mool--;}
			$this->sentence.=$this->decider(($arro[0]),0).' '.$this->mool_array[$mool];
			$mool--;
			//leave the first one and prepare string of others
			$arrlen=count($arro);
			for($i=1;$i<$arrlen;$i++)
			{
				$this->sentence.=' '.$this->decider($arro[$i],1);
				if($mool!=0)
				{
					if($arro[$i]!='000')
					{
						$this->sentence=' '.$this->sentence.' '.$this->mool_array[$mool];
					}
				}
				$mool--;
			}
		}
		return ucfirst(trim($this->sentence));
	}
}
?>
